package com.lucy.nepu.system.period;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.nepu.data.workload.entity.Workload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 * 周期相关服务实现
 */
@Slf4j
@Service
public class PeriodServiceImpl implements PeriodService{
    @Autowired
    PeriodDAO dao;

    @Override
    public Period getPeriod() {
        Date now = DateUtil.date();
        List<Period> periods = dao.findByStartDateLessThanAndEndDateGreaterThan(now,now);
        Period res = null;
        for (Period period:periods){
            // 父周期ID 为空 是考核周期
            if (Validator.isEmpty(period.getParentId())){
                res = period;
            }
        }
        if (Validator.isEmpty(res)){
            throw new BusinessException(HttpStatus.NOT_FOUND,"当前不处于考核周期内");
        }
        return res;
    }

    @Override
    public List<Period> getSubPeriod(String periodId) {
        return dao.findByParentId(periodId);
    }

    @Override
    public Period getOneUnwrap(String s) {
        Optional<Period> periodOp = dao.findById(s);
        if (!periodOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return periodOp.get();
    }


    @Override
    public PageResultDTO<Period> getAll(Pageable pageable, Example<Period> example) {
        Page<Period> page;

        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }
        else {
            page = dao.findAll(example,pageable);
        }
        /*
          获取分页主体内容
         */
        List<Period> list = page.getContent();
        PageResultDTO<Period> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<Period> getExample(Example<Period> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        dao.deleteById(id);
    }

    @Override
    public Optional<Period> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public Period saveOne(Period o) {
        if (Validator.isNotEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"新建DTO不可携带ID");
        }
        Period saveRes = dao.save(o);
        // 事件发布
        return saveRes;
    }

    @Override
    public Period updateOne(Period o) {
        if (Validator.isEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"更新DTO必须携带ID");
        }
        return dao.save(o);
    }
}
